package penguin.masking.asm;

import org.objectweb.asm.Type;

/**
 * @author wuxh
 * @version 1.0.0
 */
public class ReductionMethodVisitor extends CustomMethodVisitor {

    @Override
    public void beforeMethod() {
        //方法开始前增加逻辑
        Type[] argumentTypes = Type.getArgumentTypes(agent.getDescriptor());
//            System.out.println("ReductionMethodVisitor:" + argumentTypes[0].getClassName());
        int sr = -1;//字符串参数得位置
        int or = -1;//object参数得位置
        for (int i = 0; i < argumentTypes.length; i++) {
            if ("java.lang.String".equals(argumentTypes[i].getClassName())) {
                sr = i + 1;
                break;
            }
            if ("java.lang.Object".equals(argumentTypes[i].getClassName())) {
                or = i + 1;
                break;
            }
        }
        if (sr != -1) {
            mv.visitVarInsn(ALOAD, sr);
            mv.visitMethodInsn(INVOKESTATIC, "penguin/masking/DataReductionService", "reduction", "(Ljava/lang/String;)Ljava/lang/String;", false);
            mv.visitVarInsn(ASTORE, sr);
        }
        if (or != -1) {
            mv.visitVarInsn(ALOAD, or);
            mv.visitMethodInsn(INVOKESTATIC, "penguin/masking/DataReductionService", "reduction", "(Ljava/lang/Object;)Ljava/lang/Object;", false);
            mv.visitVarInsn(ASTORE, or);
        }
    }
}
